package com.tangkun.environment.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tangkun.environment.R;
import com.tangkun.environment.activity.QiYeZhongXinActivity;
import com.tangkun.environment.application.Session;
import com.tangkun.environment.entity.gongzuo.EnterpriseReturn;
import com.tangkun.environment.network.GlobalUrl;
import com.tangkun.environment.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author tangkun
 * @ClassName: WuRanYuanAdapter
 * @Description: 污染源列表适配器
 * @date 2016-08-20 上午11:57:36
 */
public class WuRanYuanAdapter extends BaseAdapter {

    private Context context;
    private List<EnterpriseReturn.DataBean.ListBean> dataList = new ArrayList<EnterpriseReturn.DataBean.ListBean>();

    public WuRanYuanAdapter(Context context, List<EnterpriseReturn.DataBean.ListBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int i) {
        return dataList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_wuranyuan, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (dataList != null) {
            //tangkun 暂时隐藏吧 污染源认证
//            if (dataList.get(position).sex) {
//                holder.ivWuranyuanWuranyuanrenzheng.setVisibility(View.VISIBLE);
//            } else {
            holder.ivWuranyuanWuranyuanrenzheng.setVisibility(View.INVISIBLE);
//            }
            //图片
            Glide.with(context)
                    .load(GlobalUrl.IMG_URL + dataList.get(position).img)
                    .dontAnimate()
                    .placeholder(R.mipmap.ic_place_holder)
                    .dontAnimate()
                    .into(holder.ivWuranyuanImg);
            //企业名称
            if (!TextUtils.isEmpty(dataList.get(position).name)) {
                holder.tvWuranyuanContent.setText(dataList.get(position).name);
            } else {
                holder.tvWuranyuanContent.setText("");
            }
            //地址
            if (!TextUtils.isEmpty(dataList.get(position).address)) {
                holder.tvWuranyuanDizhi.setText(dataList.get(position).address);
            } else {
                holder.tvWuranyuanDizhi.setText("");
            }
            //距离
            if (!TextUtils.isEmpty(dataList.get(position).distance)) {
                holder.tvWuranyuanJuli.setText(Utils.getDistance(dataList.get(position).distance));
            } else {
                holder.tvWuranyuanJuli.setText("");
            }

            //跳转基础资料页面
            convertView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(v.getContext(), QiYeZhongXinActivity.class);
                    i.putExtra("id", dataList.get(position).ids);
                    i.putExtra("name", dataList.get(position).name);
                    v.getContext().startActivity(i);
                }
            });
        }
        return convertView;

    }

    /**
     * 自定义接口回调
     */
    private MyClickListener myClickListener;

    public void setMyClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public interface MyClickListener {
        void onMyClick(int position);
    }

    static class ViewHolder {
        @BindView(R.id.iv_wuranyuan_img)
        ImageView ivWuranyuanImg;
        @BindView(R.id.iv_wuranyuan_wuranyuanrenzheng)
        ImageView ivWuranyuanWuranyuanrenzheng;
        @BindView(R.id.tv_wuranyuan_content)
        TextView tvWuranyuanContent;
        @BindView(R.id.tv_wuranyuan_dizhi)
        TextView tvWuranyuanDizhi;
        @BindView(R.id.tv_wuranyuan_juli)
        TextView tvWuranyuanJuli;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
